#ifndef __data_shuttle_io_loop_header_file_h__
#define __data_shuttle_io_loop_header_file_h__

#include <sys/epoll.h>
#include "task.h"
#include "timer.h"

namespace datashuttle
{

class EventLoop;

class Breaker : public EventTask
{
public:
	Breaker( IoLoop& loop);
	virtual ~Breaker();
	
	void wakeup(); //wake up EventLoop so that it can process new request which did not come from io

	virtual bool init();// do some initialization stuff
	
protected:

	virtual bool isValid( ) const{ return true;}//always return true ?
	
	virtual void run( int type );
	
	virtual void cancel(){}

	bool	createBreaker(); //创建一个用于wakeup当前EventHandle的东西，这样我们就可以在需要的时候唤醒epoll_wait

	void	clearResource();

	void	clearInterrupt();

	void	setInterrupt();

private:
	int			mBreakerHandle[2];
};
typedef Pointer<Breaker> BreakerPtr;



class EventLoop : public IoLoop
{
public:	
	EventLoop();
	virtual ~EventLoop();

public:

	bool	init( int eventHandle , BreakerPtr breaker = 0 );

	void	stop(); //这个函数的作用是通知EventLoop停止当前的工作并且推出run函数

	void	run();

	virtual bool	addFd( EventTaskPtr task );

	virtual void	removeFd( EventTaskPtr task );

	virtual bool	addTask( BaseTaskPtr task );

	virtual void	removeTask( BaseTaskPtr task );

private:

	void	clearResource(); //清理掉所有创建的资源
	
	long long	runExpiredTasks();

	void	postRun();

private:
	TimerTable	mTimerTable; //当前的EventLoop所管理的task table
	int			mEventHandle;
	BreakerPtr	mBreaker;	
	bool		mbRunning;
};

}//namespace datashuttle

#endif//__data_shuttle_io_loop_header_file_h__
//vim:tw=80:ts=4:encoding=utf-8
